package com.lt.setimage;

import android.util.DisplayMetrics;

/**
 * Created by lt on 2017/5/14.
 */

public class Utils {

    /**  complex unit: Value is raw pixels. */
    public static final int COMPLEX_UNIT_PX = 0;
    /**  complex unit: Value is Device Independent Pixels. */
    public static final int COMPLEX_UNIT_DIP = 1;
    /**  complex unit: Value is a scaled pixel. */
    public static final int COMPLEX_UNIT_SP = 2;
    /**  complex unit: Value is in points. */
    public static final int COMPLEX_UNIT_PT = 3;
    /**  complex unit: Value is in inches. */
    public static final int COMPLEX_UNIT_IN = 4;
    /**  complex unit: Value is in millimeters. */
    public static final int COMPLEX_UNIT_MM = 5;

    /**
     * Converts an unpacked complex data value holding a dimension to its final
     * floating point value. The two parameters unit and value are as in dimension.
     *
     * @param unit The unit to convert from.
     * @param value The value to apply the unit to.
     * @param metrics Current display metrics to use in the conversion --
     *                supplies display density and scaling information.
     *
     * @return The complex floating point value multiplied by the appropriate
     * metrics depending on its unit.
     */
    public static float applyDimension(int unit, float value,
                                       DisplayMetrics metrics)
    {
        switch (unit) {
            case COMPLEX_UNIT_PX: //原始像素点
                return value;
            case COMPLEX_UNIT_DIP: //设备独立点
                return value * metrics.density;
            case COMPLEX_UNIT_SP: //缩放像素点
                return value * metrics.scaledDensity;
            case COMPLEX_UNIT_PT: //英磅
                return value * metrics.xdpi * (1.0f/72);
            case COMPLEX_UNIT_IN: //英寸
                return value * metrics.xdpi;
            case COMPLEX_UNIT_MM: //毫米
                return value * metrics.xdpi * (1.0f/25.4f);
        }
        return 0;
    }


}
